﻿#include "ndt_rawdata.h"
#include "ndt_algorithm.h"
 

static bank3 int gRawData[CHANNEL_NUM];
bank3 int gRawdata_Sp[CHANNEL_NUM];
bank3 int gRawdata_Sp_Long[CHANNEL_NUM];

bank3 int gAdcFilterBackup_ch0[SMOOTH_MAX_LEVEL_CH0];  //滤波缓存数据
bank1 int gRawdataBackup_ch0[RAWDATA_BACKUP_NUM];  //rawdata缓存数据

#if CHANNEL_NUM == 2//防止占用多余空间
	bank3 int gAdcFilterBackup_ch1[SMOOTH_MAX_LEVEL_CH1];  //滤波缓存数据
	bank1 int gRawdataBackup_ch1[RAWDATA_BACKUP_NUM];  //rawdata缓存数据
#endif

bank1 int *buf;
/**
 * @brief 中值滤波
 * 
 * @param data 最新帧数据
 * @param buf  缓存的buf
 * @return int 
 */
 
 void raw_init()
{
	char i;
	char j;

	for(j = 0;j<CHANNEL_NUM;j++)
	{
		gRawdataBackup_ch0[j] = 0;
		gAdcFilterBackup_ch0[j] = 0;
		#if CHANNEL_NUM == 2
			gRawdataBackup_ch1[j] = 0;
			gAdcFilterBackup_ch1[j] = 0;	
		#endif
	}
			
}
int Eedian_Filtering(int data,bank3 int *buf)
{
	int filter_buf[SMOOTH_MAX_LEVEL];
	long int sum;
	int avg;
	int temp;
	char j,i;
	for(i= 0;i<SMOOTH_MAX_LEVEL;i++)
	{
		buf[i] = buf[i+1];
		if(i == SMOOTH_MAX_LEVEL-1)
		{
			buf[i] = data;
		}
	}
	
	
	for(i = 0;i<SMOOTH_MAX_LEVEL;i++)
	{
		filter_buf[i] = buf[i];
	}
	
	//排序
	for( i= 0;i<SMOOTH_MAX_LEVEL;i++)
	{
		for( j= 0;j<SMOOTH_MAX_LEVEL-i-1;j++)
		{
			if(filter_buf[j]<filter_buf[j+1])
			{
				temp = filter_buf[j+1];
				filter_buf[j+1] = filter_buf[j];
				filter_buf[j] = temp;
			}
		}
	}
	return filter_buf[SMOOTH_MAX_LEVEL>>1] ;
}
/**
 * @brief 平滑滤波
 * 
 * @param data 最新帧数据
 * @param buf 缓存数据的指针
 * @param len 需要滑窗的长度
 * @return int 
 */
int  Smooth_Filtering(int data,bank3 int *buf, char len,char on_off_maxmin)
{
	long int sum = 0;
	char i;
	int temp;
	int min,max;
	min = 0;
	max = 0; 
	for(i = 0;i < len;i++)
	{
		if(i != len - 1)
		{
			buf[i] = buf[i+1];
		}
		else
		{
			buf[i] = data;
		}
		
#if on_off_maxmin
		//去最大最小
		if(on_off_maxmin == 1)
		{
			if(buf[i]>max)
			{
				max = buf[i];
			}
			
			if(buf[i]<min)
			{
				min = buf[i];
			}
		}
		
#else
		sum += buf[i]; 
#endif
			
	}
#if on_off_maxmin
		temp = (sum - min - max ) / (len-2);
#else
		temp = sum  / len;
#endif	

	return temp;
}
/*
* Description   : 数据缓存
* Paramter      : none
* Return        : none
*/
void Rawdata_Bankup(int data,bank1 int *buf)
{
	char i;
	for( i = 0; i < RAWDATA_BACKUP_NUM; i++)
	{
		if(i != RAWDATA_BACKUP_NUM -1)
		{
			buf[i] = buf[i+1];
		}
		else
		{
			buf[i] = data;
		}	
	}		
}

/**
 * @brief 滤波
 * 
 */
void Adc_Smooth()
{
#if CHANNEL_NUM == 1
	gRawData[CH0] = Smooth_Filtering(Get_AdcData(CH0),gAdcFilterBackup_ch0,SMOOTH_MAX_LEVEL_CH0,0);
#else
	gRawData[CH0] = Smooth_Filtering(Get_AdcData(CH0),gAdcFilterBackup_ch0,SMOOTH_MAX_LEVEL_CH0,0);
	gRawData[CH1] =  Smooth_Filtering(Get_AdcData(CH1),gAdcFilterBackup_ch1,SMOOTH_MAX_LEVEL_CH1,0);
#endif
}

/**
 * @brief 获取raw_Data
 * 
 * @return int 返回raw_Data
 */
int Get_Rawdata(char ch_num)
{
	return gRawData[ch_num];
}
/**
 * @brief 计算数据的怎加速度
 * 
 */

void Record_Speed_Process()
{
	char i;

	for( i= 0;i<CHANNEL_NUM;i++)
	{
		if(i == 0)
		{
			buf = gRawdataBackup_ch0;
		}else{
			#if CHANNEL_NUM == 2
				buf = gRawdataBackup_ch1;
			#endif
		}
	
		gRawdata_Sp[i] = buf[RAWDATA_BACKUP_NUM -1] - buf[RAWDATA_BACKUP_NUM -1 -2];
		gRawdata_Sp_Long[i] = buf[RAWDATA_BACKUP_NUM -1] - buf[RAWDATA_BACKUP_NUM -1 -2 -4];
	}
	

}

void Data_Bankup()
{
	char i;
	
	for(i = 0; i<CHANNEL_NUM ;i++)
	{
		if(i == 0)
		{
			buf = gRawdataBackup_ch0;
		}else{
			#if CHANNEL_NUM == 2
			buf = gRawdataBackup_ch1;	
			#endif
		}
		
		Rawdata_Bankup(Get_Rawdata(i),buf);
	}

}



/**
 * @brief 数据预处理
 * 
 */
void RawData_Process()
{
	Adc_Smooth();
	
	Data_Bankup();

	Record_Speed_Process();
}


